//
//  WKBiAddressListVM.m
//  LLWallet
//
//  Created by wangfei on 2025/9/15.
//

#import "WKBiAddressListVM.h"
#import "FFBankcardListCell.h"
#import "FFBankcardListAddCell.h"
#import "WKAddressAddVC.h"

@interface WKBiAddressListVM ()

@property(nonatomic,strong) NSArray<WKBiAddressListResp*> *items;

@end

@implementation WKBiAddressListVM


- (NSArray<NSDictionary *> *)tableSectionMaps {
    __weak typeof(self) weakSelf = self;
    NSMutableArray<NSDictionary*> *itemDicts = [NSMutableArray array];
    if(self.items) {
        for (WKBiAddressListResp *item in self.items) {
            [itemDicts addObject:@{
                @"height":@(10.0f),
                @"items":@[
                        @{
                            @"class": WKBiAddressListModel.class,
                            @"address": item.address?:@"",
                            @"symbol": item.alias?:@"",
                            @"logo": item.remark?:@"",
                            @"onClick":^{
                if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_coinSelect:)]) {
                    [weakSelf.delegate ff_coinSelect:item];
                                }
                            }
                        }
                ],
            }];
        }
    }
   
    [itemDicts addObject:@{
         @"height":@(10.0f),
         @"items":@[
                 @{
                     @"class": WKBiAddressdListAddModel.class,
                     @"onClick":^{
                        [[WKNavigationManager shared] pushViewController:[WKAddressAddVC new] animated:YES];
                     }
                 }
         ],
        
    }];
    return itemDicts;
}

- (void)requestData:(void (^)(NSError * _Nullable))complete {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:@"udun/user/addresses" parameters:nil].then(^(NSDictionary *resps){
        NSArray *list = resps[@"list"];
        NSMutableArray *arr = [NSMutableArray array];
        if ([list isKindOfClass:[NSArray class]]){
            for (NSDictionary *dic in list) {
                WKBiAddressListResp *resp = [WKBiAddressListResp new];
                resp._id = [dic[@"id"] integerValue];
                resp.symbol = dic[@"symbol"];
                resp.address = dic[@"address"];
                resp.coin_type = dic[@"main_coin_type"];
                resp.remark = dic[@"remark"];
                resp.alias = dic[@"alias"];
                [arr addObject:resp];
            }
        }
        weakSelf.items = arr;
        complete(nil);
    }).catch(^(NSError *error){
        complete(error);
    });
}

@end

@implementation WKBiAddressListResp

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    WKBiAddressListResp *resp = [WKBiAddressListResp new];
//    resp._id = [dictory[@"id"] integerValue];
//    resp.symbol = dictory[@"symbol"];
//    resp.address = dictory[@"address"];
//    resp.coin_type = dictory[@"coin_type"];
    return resp;
}


@end

